package com.edu.dao.impl;

import com.edu.dao.OrdersDao;
import com.edu.entity.OrderEntity;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * com.edu.dao.impl
 * 2024/7/11 10:09
 *
 * @author Fancy
 * @version 1.0
 */
public class OrdersDaoImpl implements OrdersDao {
    private Connection connection;

    public OrdersDaoImpl(){
        try{
            this.connection = DatabaseManager.getInstance().getConnection();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int getOrderIdNoSettled(int userId) {
        try {
            String sql = "SELECT order_id FROM `Order` WHERE status = ? and user_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"pending");
            preparedStatement.setInt(2,userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt("order_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1; // 如果没有找到订单，返回-1
    }

    @Override
    public int createOrder(int userId) {
        try {
            String sql = "INSERT INTO `Order` (user_id, total_amount, status) VALUES (?, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setDouble(2, 0); // 设置第二个参数为 double 类型的 0.00
            preparedStatement.setString(3, "pending"); // 设置第三个参数为字符串 "pending"
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public LinkedList<OrderEntity> getAllOrderByUserId(int userId) {
        try {
            String sql = "SELECT * FROM `Order` WHERE user_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            LinkedList<OrderEntity> orders = new LinkedList<>();
            while (resultSet.next()) {
                OrderEntity order = new OrderEntity();
                order.setUserId(resultSet.getInt("user_id"));
                order.setOrderId(resultSet.getInt("order_id"));
                order.setOrderDate(resultSet.getTimestamp("order_date"));
                order.setStatus(resultSet.getString("status"));
                order.setTotalAmount(resultSet.getDouble("total_amount"));
                order.setAddressId(resultSet.getInt("address_id"));
                orders.add(order);
            }
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null; // 如果没有找到，返回null
    }

    @Override
    public int updateOrderStatus(int orderId, String newStatus) {
        try {
            String sql = "UPDATE `Order` SET status = ? WHERE order_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, newStatus);
            preparedStatement.setInt(2, orderId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOrder(int orderId) {
        try{
            String sql = "DELETE FROM `Order` WHERE order_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,orderId);
            return preparedStatement.executeUpdate();
        } catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
